/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lyricsguy;

import java.util.*;
import lyricsguy.lyrics.container.LyricsContainer;
import lyricsguy.lyrics.container.LyricsContainerAnalyzer;
import lyricsguy.lyrics.process.LyricsProcessor;
import lyricsguy.procress.DocumentGetter;
import lyricsguy.procress.SongLyricsGetter;
import lyricsguy.procress.SongUrlGetter;
import lyricsguy.util.comparators.MapSorter;
import lyricsguy.util.comparators.ValueComparator;
import org.jsoup.nodes.Document;

/**
 *
 * @author User
 */
public class LyricsGuy {

    private static final String KAYNE_WEST_URL = "http://www.lyricspedia.com/50-cent-lyrics/";
    private static final String KAYNE_WEST_PATTERN = "http://www.lyricspedia.com/50-cent/";
    private static final int START_INDEX_OF_SONG_URL_PART = 18;
    private static final String END_CHAR_SEQUENCE_OF_SONG_URL_PART = "/\"";
    private static Map<String, Integer> wordsMap = new HashMap();

    public static void main(String[] args) {

        //Instantiating song words container
        LyricsContainer.instance();

        //Recieving songs URL's
        List<String> songUrls = SongUrlGetter.getSongUrl(new DocumentGetter(KAYNE_WEST_URL).getDoc());

        //Creating list of song pages
        List<Document> songPages = new ArrayList();
        for (String songUrl : songUrls) {
            String actualSongUrl = KAYNE_WEST_PATTERN + songUrl.substring(START_INDEX_OF_SONG_URL_PART, songUrl.lastIndexOf(END_CHAR_SEQUENCE_OF_SONG_URL_PART));
            songPages.add(new DocumentGetter(actualSongUrl).getDoc());
            System.out.println("Added page " + actualSongUrl);

        }

        //Processing song page and adding wordsto container
        for (Document songpage : songPages) {
            System.out.println("Processing page: " + songpage.title());
            String song = new SongLyricsGetter(songpage).getSong();
            List<String> songWords = new LyricsProcessor(song).getWords();
            LyricsContainer.instance().addWords(songWords);
            System.out.println("Container size: " + LyricsContainer.instance().getWords().size());
        }

        //Analyzing words repeating
        System.out.println("Creating words map");
        LyricsContainerAnalyzer lyricsContainerAnalyzer = new LyricsContainerAnalyzer();
        lyricsContainerAnalyzer.createWordsMap();
        wordsMap = lyricsContainerAnalyzer.getWordsMap();
        System.out.println("Map size: " + wordsMap.size());

        wordsMap = new MapSorter().sortByValue(wordsMap);

        System.out.println("Sorted map size: " + wordsMap.size());
        for (String word : wordsMap.keySet()) {
            Integer repeats = wordsMap.get(word);
            System.out.println("Word '" + word + "' occurs for " + repeats + " times");
        }
    }
}
